<?php
#
# php insert_recurring_meeting.php [room id] [member id] [start time] [end time] [meeting name] [number of days] [days from now]  > [sql file name]
# e.g.
# php insert_recurring_meeting.php 1 154 10:00 10:30 "daily studio recruitment review" 94 3 > meetings.sql
#
# weekends are ignored
#
# then just run the sql file (first check for conflicts manually and remove those inserts from the file).
#
	error_reporting(E_ALL ^ E_NOTICE);

	if (sizeof($argv) < 6)
	{
		echo "Usage:\n\n";
		echo "php insert_recurring_meeting.php [room id] [member id] [start time] [end time] [meeting name] [number of days] [days from now] [granularity] > [sql file name]\n\n";
		echo "e.g.\n\n";
		echo "php insert_recurring_meeting.php 1 154 10:00 10:30 \"daily studio recruitment review\" 94 3 > meetings.sql\n";
		exit();
	}

//	print("args: ");
//	print_r($argv);

	$room_id = $argv[1];
	$member_id = $argv[2];

	$start_time = $argv[3];
	list($sh, $sm) = explode(":", $start_time);

	$end_time = $argv[4];
	list($eh, $em) = explode(":", $end_time);

	$meeting_name = $argv[5];
	
	$days_in_advance = $argv[6];

	$days_from_now = $argv[7];

	$granularity = $argv[8];

	$now = time();
	$now_day = date("d", $now);
	$now_month = date("m", $now);
	$now_year = date("Y", $now);
	
	print("#\n# $now_year-$now_month-$now_day $sh:$sm - $eh:$em for the next $days_in_advance days (starting in $days_from_now days from now)\n# '$meeting_name'\n#\n");
	
	for ($i = 0; $i < $days_in_advance; $i++)
	{
		$multiplier = $i * $granularity;
		$start = mktime($sh, $sm, 0, $now_month, $now_day + $multiplier + $days_from_now, $now_year);
		$start_date = date("Y-m-d H:i:s", $start);
		
		$end = mktime($eh, $em, 0, $now_month, $now_day + $multiplier + $days_from_now, $now_year);			
		$end_date = date("Y-m-d H:i:s", $end);
		
		$sql = "INSERT INTO exp_ncb_room_bookings SET room_idFK = '$room_id', user_idFK = '$member_id', start_time = '$start_date', end_time = '$end_date', activity_name = '".mysql_escape_string($meeting_name)."'; # ".date("D", $start);
		
		if (date("D", $start) != 'Sat' && date("D", $start) != 'Sun')
		{
			print($sql."\n");
		}
	}
?>